package com.demo.juc.futuretask;


import java.util.concurrent.*;

/**
 * @ClassName CompletableFutureApi
 * @Author bsy
 * @Description CompletableFuture  常用API
 * <p>
 * 测试下边的区别
 * thenApply()
 * handle() 异常下一步还继续执行
 * thenAccept()
 * thenRun()
 * @Date 2022/9/27 23:50
 * @Version 1.0
 */
public class CompletableFutureApi {

    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
     /*   CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);  //改成1
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "aaa";
        });
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        //触发结果
        //System.out.println(completableFuture.complete("bbb") + "\t" + completableFuture.join());
        //System.out.println(completableFuture.complete("bbb") + "\t" + completableFuture.join());


        /**
         *  thenApply 异常中断，类似于；try{}catch(){}
         *  handle  异常继续往下走   类似于：try{}finally{}
         */
        /*CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
                    int result = ThreadLocalRandom.current().nextInt(10);
                    return result;
                }, threadPool)
                .thenApply((r) -> {
                    //int i = 1/0;
                    System.out.println("加1得到：" + (r + 1));
                    return r + 1;
                }).thenApply((r) -> {
                    System.out.println("加2得到：" + (r + 2));
                    return r + 2;
                }).exceptionally(e -> {
                    try {
                        System.out.println("异常处理：" + e.getCause() + "\t" + e.getMessage());
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                    return 0;
                });*/


       /* CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
                    int result = ThreadLocalRandom.current().nextInt(10);
                    return result;
                }, threadPool)
                .handle((r, e) -> {
                    System.out.println("加1得到：" + (r + 1));
                    int i = 1 / 0;
                    return r + 1;
                }).handle((r, e) -> {
                    System.out.println("加2得到：" + (r + 2));
                    return r + 2;
                }).whenComplete((r, e) -> {
                    if (e == null) {
                        System.out.println("结果：" + r);
                    }
                }).
                exceptionally(e -> {
                    try {
                        System.out.println("异常处理：" + e.getCause() + "\t" + e.getMessage());
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                    return 0;
                });*/


        // 比较结果，谁快先执行谁
        CompletableFuture<String> futureA = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("A come in...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "A";
        });

        CompletableFuture<String> futureB = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.println("B come in...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "B";
        });


        CompletableFuture<String> result = futureA.applyToEither(futureB, f -> {
            return f + " is winner!";
        });

        System.out.println(result.join());

    }
}
